<style scoped lang="scss">
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="分类名称" prop="categoryType">
            <el-select v-model="queryParams.articleType" style="width: 200px">
               <el-option
                  v-for="item in typeOptions"
                  :key="item.id"
                  :label="item.name"
                  :value="item.articleType"
               />
            </el-select>
         </el-form-item>
         <el-form-item label="文章标题" prop="title">
            <el-input
               v-model="queryParams.title"
               placeholder="请输入文章标题"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="数据状态" clearable style="width: 200px">
               <el-option
                  label="草稿"
                  value="1"
               />
              <el-option
                label="审核中"
                value="2"
              />
              <el-option
                label="审核未通过"
                value="3"
              />
              <el-option
                label="已发布"
                value="4"
              />
            </el-select>
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd"
               v-hasPermi="['system:article-category:add']"
            >新增</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="success"
               plain
               icon="Edit"
               :disabled="single"
               @click="handleUpdate"
            >修改</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="danger"
               plain
               icon="Delete"
               :disabled="multiple"
               @click="handleDelete"
               v-hasPermi="['system:article-category:remove']"
            >删除</el-button>
         </el-col>
         <el-col :span="1.5" v-if="false">
            <el-button
               type="warning"
               plain
               icon="Download"
               @click="handleExport"
               v-hasPermi="['system:article-category:export']"
            >导出</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="warning"
               plain
               icon="Close"
               @click="handleClose"
            >关闭</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="文章编码" align="center" prop="id" />
         <el-table-column label="文章标题" align="center" prop="title" />
         <el-table-column label="文章封面" align="center" prop="dictCode">
           <el-image
             style="width: 100px; height: 100px"
             :src="url"
             :preview-src-list="srcList">
           </el-image>
         </el-table-column>
        <el-table-column label="是否公开" align="center" prop="isPublic">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.isPublic === 1">公开</el-tag>
            <el-tag type="success" v-else>不公开</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="热门" align="center" prop="customHot">
          <template #default="scope">
            <el-tag type="danger" v-if="scope.row.customHot === 1">热门</el-tag>
            <el-tag type="info" v-else>正常</el-tag>
          </template>
        </el-table-column>
         <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
              <el-tag type="info" v-if="scope.row.status === 1">草稿</el-tag>
              <el-tag type="info" v-else-if="scope.row.status === 2">审核中</el-tag>
              <el-tag type="info" v-else-if="scope.row.status === 3">审核未通过</el-tag>
              <el-tag type="success" v-else>已发布</el-tag>

            </template>
         </el-table-column>
        <el-table-column label="文章摘要" align="center" prop="articleAbstract" />
         <el-table-column label="创建时间" align="center" prop="createTime" width="180">
            <template #default="scope">
               <span>{{ parseTime(scope.row.createTime) }}</span>
            </template>
         </el-table-column>
         <el-table-column label="操作" align="center" width="160" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
               <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.pageNum"
         v-model:limit="queryParams.pageSize"
         @pagination="getList"
      />

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
         <el-form ref="dataRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="文章标题">
               <el-input v-model="form.title" :disabled="true" />
            </el-form-item>
            <el-form-item label="文章封面" prop="coverImg">
              <el-upload
                class="avatar-uploader"
                action="https://run.mocky.io/v3/9d059bf9-4660-45f2-925d-ce80ad6c4d15"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="form.coverImg" :src="form.coverImg" class="avatar" />
                <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
              </el-upload>
            </el-form-item>
            <el-form-item label="是否公开" prop="isPublic">
              <el-radio-group v-model="form.isPublic">
                <el-radio :label="1">
                  是
                </el-radio>
                <el-radio :label="0">
                  否
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="热门" prop="customHot">
              <el-radio-group v-model="form.customHot">
                <el-radio :label="1">
                  是
                </el-radio>
                <el-radio :label="0">
                  否
                </el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="状态" prop="status">
               <el-radio-group v-model="form.status">
                  <el-radio :label="1">
                    草稿
                  </el-radio>
                 <el-radio :label="2">
                   审核中
                 </el-radio>
                 <el-radio :label="3">
                   审核未通过
                 </el-radio>
                 <el-radio :label="4">
                   已发布
                 </el-radio>
               </el-radio-group>
            </el-form-item>
            <el-form-item label="文章摘要" prop="articleAbstract">
               <el-input v-model="form.articleAbstract" type="textarea" placeholder="请输入内容"></el-input>
            </el-form-item>
         </el-form>
         <template #footer>
            <div class="dialog-footer">
               <el-button type="primary" @click="submitForm">确 定</el-button>
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="Data">
import useDictStore from '@/store/modules/dict'
import { optionselect as getDictOptionselect, getType } from "@/api/client/article-category/type";
import { listData, getData, delData, addData, updateData } from "@/api/client/article-category/data";
import {getCurrentInstance, ref} from "vue";
import {useRoute} from "vue-router";

const { proxy } = getCurrentInstance();
const { sys_normal_disable } = proxy.useDict("sys_normal_disable");


const url = 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
const srcList = [
  'https://fuss10.elemecdn.com/8/27/f01c15bb73e1ef3793e64e6b7bbccjpeg.jpeg',
  'https://fuss10.elemecdn.com/1/8e/aeffeb4de74e2fde4bd74fc7b4486jpeg.jpeg'
]
const dataList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const defaultArticleType = ref("");
const typeOptions = ref([]);
const route = useRoute();

// 数据分类回显样式
const listClassOptions = ref([
  { value: "default", label: "默认" },
  { value: "primary", label: "主要" },
  { value: "success", label: "成功" },
  { value: "info", label: "信息" },
  { value: "warning", label: "警告" },
  { value: "danger", label: "危险" }
]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    params: {},
    title: undefined,
    articleType: undefined,
    status: undefined
  },
  rules: {
    coverImg: [{ required: true, message: "文章封面不能为空", trigger: "blur" }],
    isPublic: [{ required: true, message: "请选择是否公开", trigger: "blur" }],
    customHot: [{ required: true, message: "请选择是否热门", trigger: "blur" }],
    status: [{ required: true, message: "请选择状态", trigger: "blur" }],
    articleAbstract: [{ required: true, message: "文章摘要不能为空", trigger: "blur" }]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询分类类型详细 */
function getTypes(id) {
  getType(id).then(response => {
    // queryParams.value.articleType = response.data.articleType;
    defaultArticleType.value = response.data.articleType;
    getList();
  });
}

/** 查询分类类型列表 */
function getTypeList() {
  getDictOptionselect().then(response => {
    typeOptions.value = response.data;
  });
}
/** 查询分类数据列表 */
function getList() {
  loading.value = true;
  queryParams.value.params['category_id'] = route.params.categoryId
  if (queryParams.value.articleType) queryParams.value.params['article_type'] = queryParams.value.articleType
  if (queryParams.value.title) queryParams.value.params['title'] = queryParams.value.title
  if (queryParams.value.status) queryParams.value.params['status'] = queryParams.value.status
  listData(queryParams.value).then(response => {
    dataList.value = response.rows
    total.value = response.total
    queryParams.value.params = {}
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    dictCode: undefined,
    dictLabel: undefined,
    dictValue: undefined,
    cssClass: undefined,
    listClass: "default",
    dictSort: 0,
    status: "1",
    remark: undefined
  };
  proxy.resetForm("dataRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 返回按钮操作 */
function handleClose() {
  const obj = { path: "/client/article-category" };
  proxy.$tab.closeOpenPage(obj);
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  queryParams.value.articleType = defaultArticleType.value;
  handleQuery();
}
/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加分类数据";
  form.value.articleType = queryParams.value.articleType;
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.dictCode);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}
/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const id = row.id || ids.value;
  getData(id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改分类数据";
  });
}
/** 提交按钮 */
function submitForm() {
  proxy.$refs["dataRef"].validate(valid => {
    if (valid) {
      if (form.value.id != undefined) {
        updateData(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addData(form.value).then(response => {
          useDictStore().removeDict(queryParams.value.articleType);
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}
/** 删除按钮操作 */
function handleDelete(row) {
  const ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除分类编码为"' + dictCodes + '"的数据项？').then(function() {
    return delData(ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
    useDictStore().removeDict(queryParams.value.articleType);
  }).catch(() => {});
}
/** 导出按钮操作 */
function handleExport() {
  proxy.download("system/dict/data/export", {
    ...queryParams.value
  }, `dict_data_${new Date().getTime()}.xlsx`);
}

getTypes(route.params && route.params.categoryId);
getTypeList();
</script>
